//
//  FWSinaClient.m
//  FWShare
//
//  Created by 范文青 on 14-6-14.
//  Copyright (c) 2014年 fwq. All rights reserved.
//

#import "FWSinaClient.h"
#import "FWSinaStatusWithPicParam.h"
#import "FWShare.h"
#import "FWShareValues.h"
#import "FWAccountProcotol.h"
#import "FWAuthUser.h"
#include "NSMutableURLRequest+FWHTTPAddition.h"
#import "BBlock.h"
@implementation FWSinaClient
- (void)share:(NSString *)content
{
    FWAuthUser *user = [FWAuthUser userByMediaType:SINA_WEIBO];
    FWSinaStatusWithPicParam *param = [FWSinaStatusWithPicParam new];
    [param setSource:[[[FWShare shareCenter] accounts] sinaAppKey]];
    [param setAccess_token:user.token.access_token];
    [param setStatus:content];
    
    NSMutableURLRequest  *request = [NSMutableURLRequest httpRequestWithURL:[NSURL URLWithString:@"https://api.weibo.com/2/statuses/update.json"] parameters:[param dictionaryRepresentation] headers:nil httpMethod:FWHTTPMethodPOST contentType:FWPostContentTypeForm];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [BBlock dispatchOnMainThread:^{
            NSDictionary  *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            if ([self isValid:dic]) {
                if (self.shareComplete) {
                    self.shareComplete(YES,nil);
                }
            }else{
                NSError  *error = [NSError errorWithDomain:@"FWShareError" code:[[dic objectForKey:@"error_code"] integerValue] userInfo:@{@"message":[dic objectForKey:@"error"]}];
                if (self.shareComplete) {
                    self.shareComplete(NO,error);
                }
            }
        }];
    }];
}
- (void)share:(NSString *)content pic:(NSData *)pic
{
    if (pic == nil) {
        [self share:content];
        return;
    }
    FWMultiPartFileData  *fileData = [FWMultiPartFileData new];
    [fileData setMimeType:@"image/jpeg"];
    [fileData setFileName:@"image"];
    [fileData setData:pic];

    FWAuthUser *user = [FWAuthUser userByMediaType:SINA_WEIBO];
    FWSinaStatusWithPicParam *param = [FWSinaStatusWithPicParam new];
    [param setSource:[[[FWShare shareCenter] accounts] sinaAppKey]];
    [param setAccess_token:user.token.access_token];
    [param setStatus:content];
    [param setPic:fileData];
    
    NSMutableURLRequest  *request = [NSMutableURLRequest httpRequestWithURL:[NSURL URLWithString:@"https://upload.api.weibo.com/2/statuses/upload.json"] parameters:[param dictionaryRepresentation] headers:nil httpMethod:FWHTTPMethodPOST contentType:FWPostContentTypeMultiPart];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [BBlock dispatchOnMainThread:^{
            
            NSDictionary  *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            if ([self isValid:dic]) {
                if (self.shareComplete) {
                    self.shareComplete(YES,nil);
                }
            }else{
                NSError  *error = [NSError errorWithDomain:@"FWShareError" code:[[dic objectForKey:@"error_code"] integerValue] userInfo:@{@"message":[dic objectForKey:@"error"]}];
                if (self.shareComplete) {
                    self.shareComplete(NO,error);
                }
            }

        }];
    }];
}
- (BOOL)isValid:(NSDictionary *)dic{
    if (dic == nil) {
        return NO;
    }
    if ([dic objectForKey:@"error"] == nil) {
        return YES;
    }
    return NO;
}
@end
